﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Avatar.Company;
using System.Web.Security;
using AvatarHelper;
using Avatar.Model;
using Avatar.Common;

public partial class Company_Infomation : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            using (var _adc = DBHelper.GetDataContext())
            {
                DropDownListHelper.Bind(ddl_CompanyNature, _adc.CompanyNature.ToList(), "Nature", "ID");
                DropDownListHelper.Bind(ddl_Industry, _adc.Industry.ToList(), "Name", "ID");
                DropDownListHelper.Bind(ddl_CompanySize, _adc.CompanySize.ToList(), "Size", "ID");
                if (Request.QueryString["Company"] != null)
                {
                    var _company = Request.QueryString["Company"].ToString();

                    var _i = _adc.CompanyInformation.FirstOrDefault(i => i.User == _company);
                    if (_i != null)
                    {
                        ViewPanel.Visible = true;
                        UpdatePanel.Visible = false;

                        lbl_CompanyName.Text = _i.CompanyName;
                        lbl_Nature.Text = _adc.CompanyNature.FirstOrDefault(i => i.ID == _i.Nature).Nature;
                        lbl_Industry.Text = _adc.Industry.FirstOrDefault(i => i.ID == _i.Industry).Name;
                        lbl_Location.Text = _i.Area.Name;//(Convert.ToInt32(_i.Location)).Name;
                        lbl_RegisteredCapital.Text = _i.RegisteredCapital;
                        lbl_CompanySize.Text = _adc.CompanySize.FirstOrDefault(c => c.ID == _i.Size).Size;
                        lbl_DateOfEstablishment.Text = string.IsNullOrEmpty(_i.DateOfEstablishment.ToString()) ? "--" : Convert.ToDateTime(_i.DateOfEstablishment).ToShortDateString();
                        lbl_Description.Text = _i.Description;
                    }
                    else
                    {
                        ViewPanel.Visible = false;
                        UpdatePanel.Visible = true;
                    }
                }
            }
        }
    }
    protected void btn_Submit_Click(object sender, EventArgs e)
    {
        try
        {
            if (Request.QueryString["Company"] == null) throw new Exception("企业名称不能为空");
            var _company = Request.QueryString["Company"].ToString();
            CompanyInformationManager.SaveCompanyInformation(
                _company,
                txt_CompanyName.Text,
                Convert.ToInt32(ddl_CompanySize.SelectedValue),
                Convert.ToInt32(ddl_CompanyNature.SelectedValue),
                Convert.ToInt32(ddl_Industry.SelectedValue),
                Location1.City,
                string.IsNullOrEmpty(txt_DateOfEstablishment.Text) ? DateTime.MinValue : Convert.ToDateTime(txt_DateOfEstablishment.Text),
                txt_Description.Text,
                txt_RegisteredCapital.Text);

            ViewPanel.Visible = true;
            UpdatePanel.Visible = false;

            Response.Redirect(Request.Url.ToString());
        }
        catch (Exception _e)
        {
            lbl_Error.Text = _e.Message;
        }
    }
    protected void btn_Cancel_Click(object sender, EventArgs e)
    {
        ViewPanel.Visible = true;
        UpdatePanel.Visible = false;
    }
    protected void btn_Delete_Click(object sender, EventArgs e)
    {
        try
        {
            if (Request.QueryString["Company"] == null) throw new Exception("企业名称不能为空");
            var _company = Request.QueryString["Company"].ToString();
            using (var _adc = DBHelper.GetDataContext())
            {

                CompanyInformationManager.Delete(_company);

                Response.Redirect(Request.Url.ToString());
            }
        }
        catch (Exception _e)
        {
            lbl_Error.Text = _e.Message;
        }
    }
    protected void btn_Edit_Click(object sender, EventArgs e)
    {
        ViewPanel.Visible = false;
        UpdatePanel.Visible = true;
        try
        {
            if (Request.QueryString["Company"] == null) throw new Exception("企业名称不能为空");
            var _company = Request.QueryString["Company"].ToString();
            using (var _adc = DBHelper.GetDataContext())
            {
                var _i = CompanyInformationManager.GetCompanyInformation(_company);
                if (_i != null)
                {
                    txt_CompanyName.Text = _i.CompanyName;
                    ddl_CompanyNature.SelectedValue = _i.Nature.ToString();
                    ddl_Industry.SelectedValue = _i.Industry.ToString();
                    //s_from = drow0["qyfrom"].ToString() + "," + drow0["qyfromid"].ToString();
                    //mapkey_Text.Value = drow0["mapKey"].ToString();
                    txt_RegisteredCapital.Text = _i.RegisteredCapital;
                    ddl_CompanySize.SelectedValue = _i.Size.ToString();
                    txt_DateOfEstablishment.Text = _i.DateOfEstablishment.ToString();
                    txt_Description.Text = _i.Description;
                }
            }
        }
        catch (Exception _e)
        {
            lbl_Error.Text = _e.Message;
        }
    }
}
